package com.hugong.yxx.wksp.modules.controller;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hugong.yxx.wksp.common.bean.CodeBean;
import com.hugong.yxx.wksp.common.bean.JSONResult;
import com.hugong.yxx.wksp.common.utils.*;
import com.hugong.yxx.wksp.modules.service.IUserService;
import com.hugong.yxx.wksp.persistence.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.hugong.yxx.wksp.common.bean.JSONResult.renderError;
import static com.hugong.yxx.wksp.common.bean.JSONResult.renderSuccess;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yxx
 * @since 2019-08-13
 */
@RestController
@RequestMapping("/sys/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;
    @Autowired
    private SendSms sendSms;

    /**
     * 微信登录
     *
     * @param code
     * @return
     * @throws Exception
     */
    @RequestMapping("/weixinLogin")
    public JSONResult weixinLogin(String code) throws Exception {
        if (ObjectUtil.isNull(code)) {
            String redirectUrl = userService.redirectUrl();
            System.out.println(redirectUrl);
            return renderSuccess(redirectUrl);
        } else {
            return userService.weixinLogin(code);
        }
    }

    /**
     * 账号密码登录
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/phoneLogin")
    public JSONResult phoneLogin(String username, String password) {
        Assert.isNull(username, "账号不能为空！");
        User user = userService.selectOne(new EntityWrapper<User>()
                .eq("username", username)
                .eq("registerType", Setting.PHONE_REGISTER));
        if (user == null) {
            return renderError("用户不存在，登陆失败！");
        }
        if (user.getPassword().equals(password)) {
            HttpContextUtils.getHttpServletRequest().getSession().setAttribute("user", user);
            user.setPassword(null);
            return renderSuccess(user);
        }
        return renderError("密码错误，登陆失败！");
    }

    /**
     * 发送验证码
     *
     * @param cellphone
     * @return
     */
    @RequestMapping("/sendMessage")
    public JSONResult sendMessageCode(String cellphone) {
        String code = sendSms.SendSmsUtil(cellphone);
        if (ObjectUtil.isNotNull(getSession().getAttribute("codeBean"))) {
            getSession().removeAttribute("codeBean");
        }
        getSession().setAttribute("codeBean", new CodeBean(cellphone, code));
        TimerManager.getInstance().startTimerTask();
        return JSONResult.renderSuccess();
    }

    /**
     * 通过短信登录
     *
     * @param cellphone
     * @param code
     * @return
     */
    @RequestMapping("/messageLogin")
    public JSONResult messageLogin(String cellphone, String code) {
        User user = userService.selectOne(new EntityWrapper<User>()
                .eq("username", cellphone)
                .eq("registerType", Setting.PHONE_REGISTER));
        if (user == null) {
            return renderError("该手机号尚未注册，请先注册！");
        }
        return userService.messageLogin(cellphone, code);
    }

    /**
     * 手机号注册
     *
     * @param username
     * @param code
     * @param password
     * @return
     */
    @RequestMapping("/addUser")
    public JSONResult addUser(String username, String code, String password) {
        Assert.isNull(username, "账号不能为空！");
        Assert.isNull(password, "密码不能为空！");
        return userService.addUser(username, code, password);
    }

    /**
     * 用户详情
     *
     * @param userID
     * @return
     */
    @RequestMapping("/getUser")
    public JSONResult getUser(Integer userID) {
        return JSONResult.renderSuccess(userService.selectById(userID));
    }

}

